﻿using CNotebook.Sales;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class M_ADDITEM_IN_PL : RibbonForm
    {
        public M_ADDITEM_IN_PL()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string PriceListCode { get; set; }
        public string PriceListName { get; set; }

        private void M_ADDITEM_IN_PL_Load(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            PriceManager oManager = new PriceManager();
            PriceUnitLineItems oUnit = new PriceUnitLineItems();

            oUnit.PriceCode = this.PriceListCode;
        
            oUnit.PriceName = this.PriceListName;
            oUnit.ItemCode = this.txtItemCode.Text;
            oUnit.ItemName = this.txtItemName.Text;
            oUnit.Price = Convert.ToDouble(txtPrice.Text);

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            if (oManager.IsItemCodeExistsInPriceList(oUnit.PriceCode, oUnit.ItemCode) == false)
            {
                oManager.AddPrice(oUnit);
            }
            else
            {
                MessageBox.Show("Item is already exists in the Pricelist", "Price List", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            oManager.Close();

            this.Close();
        }

        private void txtItemCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_ITEM oFindItemForm = new FND_ITEM();
            oFindItemForm.ConnectionString = this.ConnectionString;
            oFindItemForm.ShowDialog();

            string sItemCode = oFindItemForm.SelectedItemCode;

            if (sItemCode != null)
            {
                ItemManager oManager = new ItemManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                DataTable oTable = oManager.GetItemMasterData(sItemCode);
                oManager.Close();


                
                txtItemCode.Text = oTable.Rows[0]["ItemCode"].ToString();
                txtItemName.Text =  oTable.Rows[0]["Description"].ToString();
                
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
